﻿using System;
using System.Windows.Forms;
using BeiraBaga.Output;
using DevExpress.XtraGrid.Views.Grid;
using E4RPPrintManager;

namespace BeiraBaga
{
    public partial class Profitability : UserControl
    {
        #region Members

        private Controller _controller;
        private DateTime _start, _end;

        #endregion

        #region Properties
        
        public Controller Controller { set { _controller = value; } }

        public GridView GridView { get { return gridView; } }

        public DateTime Start { set { _start = value.Date; } }

        public DateTime End { set { _end = value.Date; } }

        #endregion

        public Profitability()
        {
            InitializeComponent();
        }

        #region Public Methods

        public void Reload()
        {
            rentabilidadeBindingSource.DataSource = null;
            rentabilidadeBindingSource.DataSource = _controller.GetProfitability(_start, _end);
            rentabilidadeBindingSource.DataMember = "Rentabilidade";
        }

        public void PrintPreview()
        {
            var printPreviewXtraForm = new PrintPreviewXtraForm();
            var profitabilityDataSet = new ProfitabilityDataSet();
            for (var rowHandle = 0; rowHandle < gridView.DataRowCount; rowHandle++)
            {
                var row = (ProfitabilityDataSet.RentabilidadeRow)gridView.GetDataRow(rowHandle);
                profitabilityDataSet.Rentabilidade.AddRentabilidadeRow(row.Equipa, row.Artigo, row.KGsColhidos, row.Horas, row.PercentagemPremio, row.CustoHora, row.TotalHoras, row.RendimentoPorKGPorHora, row.TotalRendimento, row.ValorGlobalPremio, row.KGsComPremio, row.TotalKGsComPremio, row.PremioPorKG, row.PremioIndividual, row.DataInicial, row.DataFinal, row.Empregado);
            }
            printPreviewXtraForm.Setup(profitabilityDataSet, "ProfitabilityCrystalReport.rpt");
            printPreviewXtraForm.ShowDialog();            
        }

        #endregion        
    }
}
